import 'package:flutter/material.dart';
import 'package:play_android/com/zhihaoliang/base/BaseState.dart';

import 'package:play_android/com/zhihaoliang/const/AppStr.dart';
import 'package:play_android/com/zhihaoliang/http/HttpException.dart';
import 'package:play_android/com/zhihaoliang/play/article/view/ArticleDetailPage.dart';
import 'package:play_android/com/zhihaoliang/play/common/article/ArticleItem.dart';
import 'package:play_android/com/zhihaoliang/play/login/view/LoginPage.dart';
import 'package:play_android/com/zhihaoliang/play/presenter/LikePresenter.dart';
import 'package:play_android/com/zhihaoliang/util/StringUtils.dart';
import 'package:play_android/com/zhihaoliang/util/ToastUtil.dart';
import 'package:play_android/com/zhihaoliang/http/HttpRequest.dart';

///  ***************************************************************************
///  左面"主页"的列表中Item
///  ***************************************************************************

// ignore: must_be_immutable
class ArticleItemPage extends StatefulWidget {
  ArticleItem homeItem;

  bool isSearch;

  String id;

  ArticleItemPageListCallBack _callBack;

  ArticleItemPage(ArticleItem homeItem, [this._callBack ]) {
    this.homeItem = homeItem;
    this.isSearch = false;
    if (_callBack != null) {
      homeItem.collect = true;
    }
  }

  ArticleItemPage.isFromSearch(ArticleItem homeItem, String id) {
    this.homeItem = homeItem;
    this.isSearch = true;
    this.id = id;
  }

  @override
  State<StatefulWidget> createState() {
    return _ArticleItemPageState();
  }
}

class _ArticleItemPageState extends State<ArticleItemPage>
    with BaseState
    implements LikeModelCallBack {
  LikePresenter _likePresenter;

  @override
  void initState() {
    super.initState();
    _likePresenter = LikePresenter(this);
  }

  @override
  Widget build(BuildContext context) {
    Column column = Column(
      children: <Widget>[
        Padding(
          padding: EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
          child: _initAuthor(),
        ),
        Padding(
          padding: EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
          child: _initTitle(),
        ),
        Padding(
          padding: EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
          child: _initChapterName(),
        ),
      ],
    );

    return Card(
      elevation: 4.0,
      child: InkWell(
        child: column,
        onTap: () {
          _itemClick();
        },
      ),
    );
  }

  ///作者的行
  Row _initAuthor() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        Expanded(
          child: Row(
            children: <Widget>[
              Text(AppStr.AUTHOR_COLON),
              Text(
                widget.homeItem.author,
                style: TextStyle(color: Theme.of(context).accentColor),
              )
            ],
          ),
        ),
        Text(
          widget.homeItem.niceDate,
          style: TextStyle(color: Theme.of(context).accentColor),
        ),
      ],
    );
  }

  Row _initTitle() {
    TextSpan textSpan = widget.isSearch
        ? StringUtils.getTextSpan(widget.homeItem.title, widget.id)
        : TextSpan(text: widget.homeItem.title);
    return Row(
      children: <Widget>[
        Expanded(
          child: Text.rich(
            textSpan,
            softWrap: true,
            style: TextStyle(fontSize: 16.0, color: Colors.black),
            textAlign: TextAlign.left,
          ),
        ),
      ],
    );
  }

  ///章节的初始化
  Row _initChapterName() {
    String chapterName = widget.isSearch ? "" : widget.homeItem.chapterName;
    return Row(
      children: <Widget>[
        Expanded(
          child: Text(
            chapterName,
            softWrap: true,
            style: TextStyle(color: Theme.of(context).accentColor),
            textAlign: TextAlign.left,
          ),
        ),
        IconButton(
          icon: Icon(
            widget.homeItem.collect ? Icons.favorite : Icons.favorite_border,
            color: widget.homeItem.collect ? Colors.red : null,
          ),
          onPressed: () {
            _handleOnItemCollect(widget.homeItem);
          },
        ),
      ],
    );
  }

  void _handleOnItemCollect(ArticleItem homeItem) {
    HttpRequest().isCookie().then((isLogin) {
      if (isLogin) {
        _itemCollect();
      } else {
        _login();
      }
    });
  }

  _login() {
    Navigator.of(context).push(MaterialPageRoute(builder: (context) {
      return LoginPage();
    }));
  }

  _itemCollect() {
    showLoadingDialog(context);
    _likePresenter.load(widget.homeItem.collect, widget.homeItem.id);
  }

  _itemClick() {
    Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
      return new ArticleDetailPage(
          title: widget.homeItem.title, url: widget.homeItem.link);
    }));
  }

  @override
  void onLikeComplete() {
    dismissLoadingDialog(context);
    ToastUtil.showWarn(context, AppStr.LIKE_SUCCESS);
    if(widget._callBack != null){
      widget._callBack.onUnLickCallBack(widget.homeItem);
    }
    setState(() {
      widget.homeItem.collect = !widget.homeItem.collect;
    });
  }

  @override
  void onLikeError(HttpException httpException) {
    onTipError(httpException, context, AppStr.LIKE_FAIL);
  }

}

abstract class ArticleItemPageListCallBack{
  onUnLickCallBack(ArticleItem articleItem);
}
